export default {
    init(left,right,move_el,w) {
        let list =  {
            item_index: 0, // 列表第一个元素的下标
            item_num: 0, // 列表里有多少元素
            item_width: 25, // 元素占屏幕的百分比
            item_per_view: 0, //一个画面多少元素
            bannerTime: 0, //左右切换的节流
            bannerTimeLimit: 300, //用户连续点击的节流限制时间
            el: null,

            init: function(left,right,move_el,w) {
                this.item_width = w;
                this.el = $(move_el);
                this.item_num = this.el.children('li').length;
                this.item_per_view = Math.floor(100 / this.item_width);
                $(left).click(()=>{this.index_sub()})
                $(right).click(()=>{this.index_add()})
            },
        
            move: function() {
                // console.log(this.el);
                let leftPrec = this.item_index * this.item_width + '%';
                this.el.animate({left: '-'+leftPrec},this.bannerTimeLimit-20);
            },
        
            index_sub: function() {
                let now = new Date().getTime();
                if(now - this.bannerTime < this.bannerTimeLimit){
                    // console.log('out of time1');
                    return ;
                }
                this.bannerTime = now;
                if(this.item_index <= 0) {
                    this.item_index = 0;
                }else {
                    this.item_index --;
                }
                this.move();
            },
            index_add: function() {
                let now = new Date().getTime();
                if(now - this.bannerTime < this.bannerTimeLimit){
                    // console.log('out of time1');
                    return ;
                }
                this.bannerTime = now;
                if(this.item_index >= this.item_num - this.item_per_view) {
                    this.item_index = this.item_num - this.item_per_view;
                }else {
                    this.item_index ++;
                }
                this.move();
            }
        }

        list.init(left,right,move_el,w);
        return list
    },
}